package cn.nku.shichangwei;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeachDao extends BaseDao{
	public boolean addTeach(Teach tea){
		String sql="insert into teach values(?,?)";
		try {
			java.sql.PreparedStatement pre=con.prepareStatement(sql);
			pre.setString(1,tea.getTeaid());
			pre.setString(2,tea.getCouid());
			if(pre.executeUpdate()>0)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public List<Teach> selectTeach(Teach t) {
		// TODO Auto-generated method stub
		String sql="select * from teach ";
		List<Teach> retList=new ArrayList<>();
		if(t.getTeaid()!=null&&t.getTeaid().length()!=0){
			sql+="where teaid="+t.getTeaid();
		}
		java.sql.PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			ResultSet executeQuery=pre.executeQuery();
			while(executeQuery.next()){
				Teach te=new Teach();
				te.setTeaid(executeQuery.getString("teaid"));
				te.setCouid(executeQuery.getString("couid"));
				retList.add(te);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retList;
	}

	public boolean update(Teach t) {
		// TODO Auto-generated method stub
		String sql = "update teach set couid=? where teaid=?";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, t.getCouid());
			preparedStatement.setString(2, t.getTeaid());
			if(preparedStatement.executeUpdate() > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public boolean delete(String id) {
		// TODO Auto-generated method stub
		String sql="delete from teach where teaid=?";
		java.sql.PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, id);
			if(pre.executeUpdate()>0)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
